package com.sky.fillCommonFields;

import com.sky.constant.AutoFillConstant;
import com.sky.context.BaseContext;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Method;
import java.time.LocalDateTime;

/**
 * @Author
 * @Date 2024/4/27 21:13
 * @Description: 公共字段自动填充
 */
@Slf4j
public class FillCommonFieldsInsert {
    public static void AutoFill(Object object){
        log.info("公共字段自动填充");
        LocalDateTime now = LocalDateTime.now();
        Long userId = BaseContext.getCurrentId();

        try {
            Method setCreateTime = object.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
            Method setUpdateTime = object.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
            Method setCreateUser = object.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_USER, Long.class);
            Method setUpdateUser = object.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);

            setCreateTime.invoke(object, now);
            setUpdateTime.invoke(object, now);
            setCreateUser.invoke(object, userId);
            setUpdateUser.invoke(object, userId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
